/**
 * 通用工具
 * @param params
 */


export function tansParams(params: object | any):string {
    let result = ''
    for (const propName of Object.keys(params)) {
        const value = params[propName];
        var part = encodeURIComponent(propName) + "=";
        if (value !== null && typeof (value) !== "undefined") {
            if (typeof value === 'object') {
                for (const key of Object.keys(value)) {
                    if (value[key] !== null && typeof (value[key]) !== 'undefined') {
                        let params = propName + '[' + key + ']';
                        var subPart = encodeURIComponent(params) + "=";
                        result += subPart + encodeURIComponent(value[key]) + "&";
                    }
                }
            } else {
                result += part + encodeURIComponent(value) + "&";
            }
        }
    }
    return result
}


export function handleTree (data:any, id:string, parentId:string, children:string, rootId:any) {
    id = id || 'id'
    parentId = parentId || 'parentId'
    children = children || 'children'
    rootId = rootId || Math.min.apply(Math, data.map((item:any) => { return item[parentId] })) || 0
    // 对源数据深度克隆
    const cloneData = JSON.parse(JSON.stringify(data))
    // 循环所有项
    const treeData = cloneData.filter((father:any) => {
        var branchArr = cloneData.filter((child:any) => {
            // 返回每一项的子级数组
            return father[id] === child[parentId]
        })
        if (branchArr.length > 0) {
            father.children = branchArr
        } else {
            father.children = ''
        }
        // 返回第一层
        return father[parentId] === rootId
    })
    return treeData !== '' ? treeData : data
}
